<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRulesTable extends Migration
{
    /**
     * 数据表名称
     * @var string
     */
    private $tableName = 'rules';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 创建数据表
        Schema::create($this->tableName, function (Blueprint $table) {
            // 数据表类型
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

            // 数据表字段
            $table->increments('rule_id')->comment('递增主键');
            $table->string('rule_name', 20)->comment('节点名称');
            $table->integer('pid', false, true)->default(0)->comment('上级主键');
            $table->string('uri', 150)->comment('请求路径');
            $table->integer('sort', false, true)->default(50)->comment('排序数值（升序）');
            $table->tinyInteger('status', false, true)->default(1)->comment('可用状态 0:停用 1:正常');
            $table->dateTime('created_at')->comment('数据创建时间');
            $table->timestamp('updated_at')->comment('最后更新时间')->default(DB::raw('CURRENT_TIMESTAMP'));

            // 数据表索引
            $table->index('rule_id');
            $table->index('pid');
            $table->index('uri');
        });
        // 设置数据表备注信息
        DB::statement("ALTER TABLE `".env('DB_PREFIX')."{$this->tableName}` COMMENT'权限节点表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
